using System;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Web.Mail;
using System.Data;
using System.Data.SqlClient;
using System.EnterpriseServices;


namespace MailQueueProcessingLib
{
    public enum MailStatus {Pending, Sent, Warning, Error};

	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[Transaction(TransactionOption.Supported)]
	public class MailHelper : ServicedComponent
	{
		public MailHelper()
		{
			//
			// Private Constructor
			//			
		}

		public static void SaveMail(string fromName, string fromAddress, string toList, string ccList,string bccList,
                                     string subject, int importance, string body, string label, int foreignKey1, int foreignKey2, string foreignKey3)
		{
			System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
			string connString =  ((string)(configurationAppSettings.GetValue("connectionString", typeof(string))));
			SqlConnection sqlConn = new SqlConnection(connString);
			sqlConn.Open();
			SqlCommand command = new SqlCommand("usp_AddMailMessage", sqlConn);
			command.CommandType = CommandType.StoredProcedure;

            //Set the From Name Parameter
            SqlParameter paramFromName = new SqlParameter("@fromName", SqlDbType.VarChar, 50);
            paramFromName.Direction = ParameterDirection.Input;
            paramFromName.Value = fromName;
            command.Parameters.Add(paramFromName);

			//Set the From Parameter
			SqlParameter paramFromMailAddress = new SqlParameter("@fromMailAddress", SqlDbType.VarChar,50);
			paramFromMailAddress.Direction = ParameterDirection.Input;
            paramFromMailAddress.Value = fromAddress;
			command.Parameters.Add(paramFromMailAddress);
			
			//Set the To Parameter
			SqlParameter paramToMailAddress = new SqlParameter("@toMailAddress", SqlDbType.VarChar,1028);
			paramToMailAddress.Direction = ParameterDirection.Input;
			paramToMailAddress.Value = toList;
			command.Parameters.Add(paramToMailAddress);

			//Set the CC Parameter
			SqlParameter paramCCMailAddress = new SqlParameter("@ccList", SqlDbType.VarChar,1028);
			paramCCMailAddress.Direction = ParameterDirection.Input;
			paramCCMailAddress.Value = ccList;
			command.Parameters.Add(paramCCMailAddress);

			//Set the BCC Parameter
			SqlParameter paramBccMailAddress = new SqlParameter("@bccList", SqlDbType.VarChar,1028);
			paramBccMailAddress.Direction = ParameterDirection.Input;
			paramBccMailAddress.Value = bccList;
			command.Parameters.Add(paramBccMailAddress);

			//Set the Subject Parameter
			SqlParameter paramSubject = new SqlParameter("@subject", SqlDbType.VarChar,256);
			paramSubject.Direction = ParameterDirection.Input;
			paramSubject.Value = subject;
			command.Parameters.Add(paramSubject);

			//Set the Priority Parameter
			SqlParameter paramPriority = new SqlParameter("@priority", SqlDbType.Int);
			paramPriority.Direction = ParameterDirection.Input;
			paramPriority.Value = importance;
			command.Parameters.Add(paramPriority);

			//Set the Body Parameter
			SqlParameter paramBody = new SqlParameter("@body", SqlDbType.VarChar,8000);
			paramBody.Direction = ParameterDirection.Input;
			paramBody.Value = body;
			command.Parameters.Add(paramBody);

			//Set the Attachments parameter
			SqlParameter paramAttachments = new SqlParameter("@attachments", SqlDbType.VarChar,256);
			paramAttachments.Direction = ParameterDirection.Input;
			paramAttachments.Value = "";
			command.Parameters.Add(paramAttachments);

			//Set the MailSent parameter
			SqlParameter paramMailSent = new SqlParameter("@mailSent", SqlDbType.TinyInt);
			paramMailSent.Direction = ParameterDirection.Input;
			paramMailSent.Value = 0;
			command.Parameters.Add(paramMailSent);

            //Set the label parameter
            SqlParameter paramLabel = new SqlParameter("@label", SqlDbType.VarChar);
            paramLabel.Direction = ParameterDirection.Input;
            paramLabel.Value = label;
            command.Parameters.Add(paramLabel);

            //Set the label parameter
            SqlParameter paramFk1 = new SqlParameter("@fk1", SqlDbType.Int);
            paramFk1.Direction = ParameterDirection.Input;
            paramFk1.Value = foreignKey1;
            command.Parameters.Add(paramFk1);

            //Set the label parameter
            SqlParameter paramFk2 = new SqlParameter("@fk2", SqlDbType.Int);
            paramFk2.Direction = ParameterDirection.Input;
            paramFk2.Value = foreignKey2;
            command.Parameters.Add(paramFk2);

            ///Set the label parameter
            SqlParameter paramFk3 = new SqlParameter("@fk2", SqlDbType.VarChar);
            paramFk3.Direction = ParameterDirection.Input;
            paramFk3.Value = foreignKey3;
            command.Parameters.Add(paramFk3);
			
			//Execute the stored procedure
            command.ExecuteNonQuery();

            sqlConn.Close();
		}		
	}
}
